package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.sys.modular.system.entity.BtyRbsc;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyRbscMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BtyRbscService {

    @Autowired
    private BtyRbscMapper btyRbscMapper;

    /**
     * 获取日报审查数据
     */
    public List<BtyRbsc> list(BtyRbsc btyRbsc) {
        //Page page = LayuiPageFactory.defaultPage();
        return btyRbscMapper.rbscList(btyRbsc);
    }

    /**
     * 获取日报审查数据
     */
    public String selectDate() {
        return btyRbscMapper.selectDate();
    }

    /**
     * 导入excel
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Boolean batchImport(String fileName, MultipartFile file) throws Exception {
        int id = 1;
        String a = btyRbscMapper.selectId();
        if (a != null) {
            id = Integer.parseInt(btyRbscMapper.selectId()) + 1;
        }
        boolean notNull = false;
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        //excel中多个sheet表，获取所有sheet表的数量
        int sheets = wb.getNumberOfSheets();
        for (int i = 0; i < sheets; i++) {
            Sheet sheet = wb.getSheetAt(i);
            if (sheet != null) {
                notNull = true;
            }

            int lastRowNum = sheet.getLastRowNum() - 1;
            for (int r = 1; r <= lastRowNum; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                //获取单元格
                Cell cell = row.getCell(0);
                if (ToolUtil.isNotEmpty(cell)) {
                    cell.setCellType(CellType.STRING);
                }
                String date = row.getCell(4) == null ? "" : row.getCell(4).getStringCellValue().replaceAll("年", "-").replaceAll("月", "-").replaceAll("日", "");
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
                Date loanDate1 = null;
                if (ToolUtil.isNotEmpty(date)) {
                    loanDate1 = df.parse(date);
                }
                DataFormatter dataFormatter = new DataFormatter();
                String reporter = row.getCell(1) == null ? "" : row.getCell(1).getStringCellValue();
                String storeCode = row.getCell(6) == null ? "" : dataFormatter.formatCellValue(row.getCell(6));
                String storeName = row.getCell(7) == null ? "" : row.getCell(7).getStringCellValue();
                String comments = row.getCell(24) == null ? "" : row.getCell(24).getStringCellValue();
                BtyRbsc btyRbsc = new BtyRbsc();
                btyRbsc.setId(id);
                btyRbsc.setDate(loanDate1);
                btyRbsc.setReporter(reporter);
                btyRbsc.setStoreCode(storeCode);
                btyRbsc.setStoreName(storeName);
                btyRbsc.setComments(comments);
                btyRbscMapper.insert(btyRbsc);
                id += 1;
            }
        }
        return notNull;
    }
}
